<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>  

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>作品图片管理</title>
    <c:set var="contextPath" value="${pageContext.request.contextPath}" scope="request"/>
	<link rel="stylesheet" href="${contextPath}/static-resources/css/index.css"/>
	<script src="${contextPath}/static-resources/jquery-1.12.1.js"></script>
	<script src="${contextPath}/static-resources/js/index.js"></script>
	
	<link rel="stylesheet" type="text/css" href="${contextPath}/static-resources/themes/material/easyui.css">
	<link rel="stylesheet" type="text/css" href="${contextPath}/static-resources/themes/icon.css">
	<script type="text/javascript" src="${contextPath}/static-resources/jquery.min.js"></script>
	<script type="text/javascript" src="${contextPath}/static-resources/jquery.easyui.min.js"></script>
	<script type="text/javascript">
		$(function(){
			$('#imageTable').datagrid({
				onDblClickRow: function(index,row){
					console.log(row.logo);
					$('#img').prop('src', row.url);
					$('#showImgWin').window('open');
				}
			});
		})
		
		function openAddImageWin(data){
			$('#addImageWin').window('open');
			$('#production').combobox('reload');
			if(data){
				$('#addImageWin').window('setTitle', '修改图片');
				$('#id').val(data.id);
				$('#url').filebox('setText', data.url);
				var fileInputId = '#' + $('#url').next().find('input[type="file"]').attr("id");
				$(fileInputId).val(null);
				$('#exhibition').combobox('setValue', data.exhibitionId);
				$('#description').textbox('setValue', data.description);
				$('#zindex').numberspinner('setValue', data.zindex);
			}else{
				$('#addImageWin').window('setTitle', '添加图片');
				$('#id').val('');
				$('#url').filebox('setText', '');
				var fileInputId = '#' + $('#url').next().find('input[type="file"]').attr("id");
				$(fileInputId).val(null);
				$('#exhibition').combobox('setValue', '');
				$('#description').textbox('setValue', '');
				$('#zindex').numberspinner('setValue', '');
			}
		}
		
		function removeImage(){
			var row = $('#imageTable').datagrid('getSelected');
			if (!row) {
				showWarn('请选择要删除的数据');
				return;
			}
			confirm('确定删除选择数据?', function(){
				openMask();
				$.ajax({
					type : "POST",
					url : '${contextPath}/image/delete.do',
					data : {"id" : row.id},
					success : function(data) {
						if (data.status) {
							closeMask();
							$('#imageTable').datagrid('reload');
						} else {
							showError(data.message);
						}
						closeMask();
					},
					error : function() {
						showError('系统异常');
						closeMask();
					}
				});				
			}, function(){
				return;
			})
		}
		
		function openUpdateImageWin(){
			var row = $('#imageTable').datagrid('getSelected');
			if (!row) {
				showWarn('请选择要修改的数据');
				return;
			}
			
			openMask();
			$.ajax({
				type : "POST",
				url : '${contextPath}/image/findOne.do',
				data : {"id" : row.id},
				success : function(data) {
					if (data.status) {
						closeMask();
						openAddImageWin(data.data);
					} else {
						showError(data.message);
					}
					closeMask();
				},
				error : function() {
					showError('系统异常');
					closeMask();
				}
			});
		}
		
		function saveImage(dataObj){
			openMask();
			$.ajax({
				type : "POST",
				url : '${contextPath}/image/save.do',
				enctype : 'multipart/form-data',
				data : dataObj,
				success : function(data) {
					if (data.status) {
						$('#addImageWin').window('close');
						$('#imageTable').datagrid('reload');
					} else {
						showError(data.message);
					}
					closeMask();
				},
				error : function() {
					showError('系统异常');
					closeMask();
				}
			});
		}

		function addImage() {
			var id = $('#id').val();
			var exhibitionId = $('#exhibition').combobox('getValue');
			var description = $('#description').textbox('getValue');
			var fileInputId = $('#url').next().find('input[type="file"]').attr("id");
			var file = document.getElementById(fileInputId).files[0];
			var zindex = $('#zindex').numberspinner('getValue');

			if (!exhibitionId) {
				showInfo("所属展览不能为空");
				return;
			}
			if (!fileInputId) {
				showInfo("作品图片不能为空");
				return;
			}
			
			var dataObj = new Object();
			dataObj.id = id;
			dataObj.exhibitionId = exhibitionId;
			dataObj.description = description;
			dataObj.zindex = zindex;
			
			if(file){
	 			var reader = new FileReader();
				reader.onload = function(e) {
					var base64stream = this.result;
					dataObj.fileName = file.name;
					dataObj.base64stream = base64stream;
					saveImage(dataObj);
				};
				reader.readAsDataURL(file);
			}else{
				saveImage(dataObj);
			}
		}
		
	</script>
</head>
<body style="height: 100%;">
	<div class="find_nav">
	    <div class="find_nav_left">
	        <div class="find_nav_list">
	            <ul>
	                <li><a href="javascript:void(0)" url="${contextPath}/exhibition/index.do">展览</a></li>
	                <li><a href="javascript:void(0)" url="${contextPath}/exhibitionHome/index.do">展厅</a></li>
	                <li><a href="javascript:void(0)" url="${contextPath}/production/index.do">作品</a></li>
	                <li class="selected"><a href="javascript:void(0)" url="${contextPath}/image/index.do">图片</a></li>
	                <li><a href="javascript:void(0)" url="${contextPath}/user/index.do">用户</a></li>
	                <li><a href="javascript:void(0)" url="${contextPath}/banner/index.do">banner</a></li>
	                <li class="sideline"></li>
	            </ul>
	        </div>
	    </div>
	</div>
	<div id="content" style="width: 100%; position: absolute;top: 45px;">
		<table id="imageTable" class="easyui-datagrid" title="作品" style="width:100%;height:100%;"
			data-options="rownumbers:true, singleSelect:true, url:'${contextPath}/image/listJson.do', method:'get',
			toolbar:'#imageTableTB' " >
			<thead>
				<tr>
					<th data-options="field:'ck', checkbox:true"></th>
					<th data-options="field:'url', width:'20%'">图片地址</th>
					<th data-options="field:'exhibition', width:'20%'" formatter = "objectFormatter">所属展览</th>
					<th data-options="field:'description', width:'25%'">描述</th>
					<th data-options="field:'zindex', width:'5%'">序号</th>
					<th data-options="field:'lightSpotSum', width:'5%'">亮点数量</th>
				</tr>
			</thead>
		</table>
		
		<div id="imageTableTB">
			<a href="#" class="easyui-linkbutton" iconCls="icon-sum" plain="true" onclick="$('#imageTable').datagrid('reload')">查询所有</a>
         	<span class="datagrid-btn-separator" style="vertical-align: middle;display:inline-block;float:none"></span>
			<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="openAddImageWin()">添加</a>
			<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="openUpdateImageWin()">编辑</a>
			<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeImage()">删除</a>
		</div>
		
		<!-- ##############下面是添加作品图片相关的窗体############## -->
		<div id="addImageWin" class="easyui-window" title="添加作品图片" data-options="iconCls:'icon-add', collapsible:'false', modal:'true', closed:true, footer:'#addImageWinFooter'" style="width:450px; height:350px; padding:10px;" >
			<div style="margin-bottom:5px">
				<input id="id" name="id" hidden="hidden"/>
				<input class="easyui-combobox" id="exhibition"  data-options="label: '所属展览：', labelPosition:'before', url:'${contextPath}/exhibition/listJson.do',method:'get', valueField:'id', textField:'name'" style="width: 100%;"/>
			</div>
			<div style="width: 100%; margin-bottom: 5px;">
				<input class="easyui-filebox" id="url" style="width:100%;" data-options="label:'作品图片：', labelPosition:'before', buttonText:'请选择...' ">
			</div>
			<div style="margin-bottom:5px">
				<input class="easyui-textbox" id="description" data-options="label: '图片描述：', labelPosition:'before', multiline:'true', height:'80'" style="width: 100%;"/>
			</div>
			<div style="margin-bottom:5px">
				<input class="easyui-numberspinner" id="zindex" data-options="label: '排序号：', labelPosition:'before', min:0, max:999" style="width: 100%;"/>
			</div>
		</div>
		
		<div id="addImageWinFooter" class="windowFooter">
			<a href="#" class="easyui-linkbutton" icon="icon-ok" onclick="addImage()">确定</a>  
            <a href="#" class="easyui-linkbutton" icon="icon-cancel" onclick="$('#addImageWin').window('close')">取消</a>  
		</div>
		
		<!-- ##############显示图片的窗口############## -->
		<div id="showImgWin" class="easyui-window" title="图片预览" data-options="iconCls:'icon-add', collapsible:'false', modal:'true', closed:true, footer:'#showImgWinFooter'" style="width:500px; height:500px; padding:10px;" >
			<div style="width: 100%; height: 100%;">
				<img id="img" alt="" src="" />
			</div>
		</div>
		<div id="showImgWinFooter" class="windowFooter">
            <a href="#" class="easyui-linkbutton" icon="icon-cancel" onclick="$('#showImgWin').window('close')">取消</a>  
		</div>
		
	</div>
</body>


</html>